package place;

import com.alibaba.fastjson.JSON;
import com.quanyan.api.APIResponse;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.utils.MD5Utils;
import com.quanyan.common.utils.PropUtils;
import com.quanyan.place.biz.IBaseService;
import com.quanyan.place.biz.ISearchService;
import com.quanyan.place.cache.PlaceRedisTemplate;
import com.quanyan.place.client.PlaceClient;
import com.quanyan.place.entity.TbBizPlace;
import com.quanyan.place.entity.TbBizPlaceExample;
import com.quanyan.place.entity.TbCity;
import com.quanyan.place.entity.apiresp.RespDistrict;
import com.quanyan.place.mapper.TbBizPlaceMapper;
import com.quanyan.search.entity.request.ReqPlaceSearchInfo;
import com.quanyan.search.entity.vo.PlaceIndexInfo;
import com.quanyan.search.service.PlaceSearchService;
import com.quanyan.search.service.impl.PlaceSearchServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

/**
 * SolrTest
 *
 * @author Tu Chun
 * @date 2016/6/12
 */

public class SolrTest2 extends BaseTest{
    //private final static String url = "http://192.168.30.181:8080/solr";
    public final static String url = "192.168.30.75:2181,192.168.30.75:2182,192.168.30.75:2183";

    private PlaceSearchService placeSearchService=new PlaceSearchServiceImpl(PropUtils.getProperty("SOLRCLOUD.SERVER.URL"));

    @Autowired
    ISearchService searchService;
    @Autowired
    TbBizPlaceMapper tbBizPlaceMapper;
    @Autowired
    IBaseService baseService;
    @Value("SOLRCLOUD.SERVER.URL")
    String solrUrl;
    @Autowired
    private PlaceClient placeClient;
    @Autowired
    private PlaceRedisTemplate placeRedisTemplate;
    @Autowired
    IBaseService iBaseService;

    @Test
    public void test33(){
        placeClient.findEarlyPlace(1082);
    }


    @Test
    public void test55(){
        RespDistrict respDistrict = this.placeClient.getRespDistrict(30.170845173150724, 120.32355846334247);
        System.out.println("SolrTest2.test55" + JSON.toJSONString(respDistrict));
    }

    @Test
    public void test66(){
        Double lng = 104.0600000000000;//116.46
        Double lat = 30.6700000000000;//39.92
        APIResponse<?> resp = iBaseService.getCitys(null,true,true,lng,lat);
        System.out.println(JSON.toJSONString(resp.getData()));
    }

    @Test
    public void test11(){
        StringBuilder sb = new StringBuilder();
        TbBizPlaceExample tbBizPlaceExample = new TbBizPlaceExample();
        tbBizPlaceExample.createCriteria();
        List<TbBizPlace> tbBizPlaces = tbBizPlaceMapper.selectByExample(tbBizPlaceExample);
        for(TbBizPlace tbBizPlace : tbBizPlaces){
            Double lng = tbBizPlace.getLng();
            Double lat = tbBizPlace.getLat();
            APIResponse<?> resp = iBaseService.getCitys(null,true,true,lng,lat);
            if (resp.isRet()){
                Map map = (Map) resp.getData();
                TbCity tbCity = (TbCity) map.get("city");
                if (null == tbCity){
                    sb.append(tbCity.getId() + ",");
                }
            }
        }
        System.out.println("================" + sb.toString());
    }

    @Test
    public void query3(){
//        ReqPlaceSearchInfo reqPlaceSearchInfo=new ReqPlaceSearchInfo();
        /**
         * 排序id 1、智能排序 2、价格排序 3、离我最近 4、人气最高
//         * 如果没有传值默认为1
         */
        /*reqPlaceSearchInfo.setPlaceKeyName("公园");
        //reqPlaceSearchInfo.setCategoryId(1);
        reqPlaceSearchInfo.setDate("2015-08-15");
        List<String> startTimeList=new ArrayList<String>();
        startTimeList.add("15:00:00");
        startTimeList.add("18:00:00");
        startTimeList.add("20:00:00");
        reqPlaceSearchInfo.setStartTimeList(startTimeList);
        reqPlaceSearchInfo.setLat(39.843485);
        reqPlaceSearchInfo.setLng(116.471228);
        reqPlaceSearchInfo.setSortId(1);
        placeSearchService.searchPlaceInfoList(reqPlaceSearchInfo);*/
        new Runnable() {
            @Override
            public void run() {
                for (int i=0; i<10; i++){
                    long aLong = ThreadLocalRandom.current().nextLong(5);
                    int u = ThreadLocalRandom.current().nextInt(100);
                    String random = RandomStringUtils.randomNumeric(2);
                    System.out.println("=============" + StringUtils.leftPad(ThreadLocalRandom.current().nextInt(100)+"", 2, "0")+ "::::" + random);

                }
            }
        }.run();

    }

    /**
     * {
     "cityId": 1,
     "placeName":"aa",
     "placeId":12700,
     "searchType":1,
     "lat":39.843485,
     "lng":116.471228,
     "querydate":1465886981023,
     "timeList":[{
     "startTime":"09:00:00",
     "endTime":"24:00:00"
     }]
     }

     {
     "categoryId":1,
     "pageNum": 1,
     "pageSize": 20,
     "searchType": 1,
     "lat":39.896698,
     "lng":116.331649,
     "querydate":1465886981023,
     "timeList":[{
     "startTime":"09:00:00",
     "endTime":"18:00:00"
     }]
     }
     */
    @Test
    public void query1(){
        placeSearchService.searchPlaceInfoBefore14Day();
    }

    @Test
    public void query4(){
        ReqPlaceSearchInfo reqPlaceSearchInfo=new ReqPlaceSearchInfo();
        /**
         * 排序id 1、智能排序 2、价格排序 3、离我最近 4、人气最高
         * 如果没有传值默认为1
         */
        reqPlaceSearchInfo.setPlaceKeyName("aa");
        //reqPlaceSearchInfo.setCategoryId(1);
        reqPlaceSearchInfo.setDate("2016-06-06");
        List<String> startTimeList=new ArrayList<String>();
        startTimeList.add("09:00:00");

        reqPlaceSearchInfo.setStartTimeList(startTimeList);
        reqPlaceSearchInfo.setLat(39.843485);
        reqPlaceSearchInfo.setLng(116.471228);
        reqPlaceSearchInfo.setSortId(1);
        placeSearchService.searchPlaceInfoList(reqPlaceSearchInfo);
    }

    @Test
    public void importALLPlace() {
        List<PlaceIndexInfo> inserPlaceIndexInfos = new ArrayList<>();
//        PlaceIndexInfo placeIndexInfo = this.searchService.getPlaceIndexInfo(12700);
        TbBizPlaceExample example = new TbBizPlaceExample();
        example.createCriteria().andIsDeleteEqualTo(0).andSourceIdEqualTo(2).andIsSignEqualTo(Constants.IS_DELETE_TRUE);
        List<TbBizPlace> tbBizPlaces = this.tbBizPlaceMapper.selectByExample(example);
        for (TbBizPlace tbBizPlace : tbBizPlaces) {

//            inserPlaceIndexInfos.add(this.baseService.tb2InsertPlaceIndexInfo(tbBizPlace, 7));
        }
        placeSearchService.insertDocs(inserPlaceIndexInfos);
    }

    @Test
    public void importOnePlace() {
        List<PlaceIndexInfo> inserPlaceIndexInfos = new ArrayList<>();
//        PlaceIndexInfo placeIndexInfo = this.searchService.getPlaceIndexInfo(12700);
        TbBizPlaceExample example = new TbBizPlaceExample();
        example.createCriteria().andIsDeleteEqualTo(0).andSourceIdEqualTo(2).andIsSignEqualTo(Constants.IS_DELETE_TRUE)
                .andIdEqualTo(12700);
        List<TbBizPlace> tbBizPlaces = this.tbBizPlaceMapper.selectByExample(example);
        for (TbBizPlace tbBizPlace : tbBizPlaces) {

//            inserPlaceIndexInfos.add(this.baseService.tb2InsertPlaceIndexInfo(tbBizPlace, 7));
        }
        placeSearchService.insertDocs(inserPlaceIndexInfos);
    }

    @Test
    public void tesRespDistrict() throws IOException {
        RespDistrict user = this.placeClient.getRespDistrict(30.66605,104.067351);
        System.out.println("==================================");
        System.out.println(JSON.toJSONString(user));
        System.out.println("==================================");
    }


    @Test
    public void test1(){
        Date preDate = (Date)placeRedisTemplate.get("kkkkuuuuu111222");
        Date curDate = new Date();
        if(preDate == null){
            placeRedisTemplate.set("kkkkuuuuu111222", curDate);
        }else {
            long diffLongTime = curDate.getTime()-preDate.getTime();
            long minuteFine = 1000;
            if(diffLongTime>0 && diffLongTime>=minuteFine){
                System.out.println("-------curDate: " + curDate.toLocaleString());
                System.out.println("----------preDate: " + preDate.toLocaleString());
            }
        }
    }

    @Test
    public void test2(){

       String str =  MD5Utils.MD5Encode(54668 + "2016-10-31" + "08:00:00");
        System.out.println("=============" + str);
    }

    @Test
    public void test3(){
        List<TbBizPlace> tbBizPlaces = new ArrayList<>();
        for(int i=0; i<2; i++){
            TbBizPlace tbBizPlace = new TbBizPlace();
            tbBizPlace.setId(i);
            tbBizPlaces.add(tbBizPlace);
        }
        for(int i=0; i<3; i++){
            TbBizPlace tbBizPlace = new TbBizPlace();
            tbBizPlace.setId(i);
            tbBizPlaces.add(tbBizPlace);
        }
        for(int i=6; i<8; i++){
            TbBizPlace tbBizPlace = new TbBizPlace();
            tbBizPlace.setId(i);
            tbBizPlace.setName("place_" + i);
            tbBizPlaces.add(tbBizPlace);
        }
        System.out.println(JSON.toJSONString(tbBizPlaces));

        List<TbBizPlace> listWithoutDup1 = new ArrayList<>(new HashSet<>(tbBizPlaces));
        List<TbBizPlace> listWithoutDup = new ArrayList<>(new HashSet<>(tbBizPlaces));
                tbBizPlaces = listWithoutDup1;
        System.out.println(JSON.toJSONString(tbBizPlaces));


        listWithoutDup.remove(listWithoutDup.size()-1);
        listWithoutDup.remove(listWithoutDup.size()-1);
        System.out.println(JSON.toJSONString(listWithoutDup));

        Collection subtractCollection = CollectionUtils.subtract(tbBizPlaces, listWithoutDup);
        System.out.println(JSON.toJSONString(subtractCollection));

        Collection unionCollection = CollectionUtils.union(tbBizPlaces, listWithoutDup);
        System.out.println(JSON.toJSONString(unionCollection));

        Collection intersectionCollection = CollectionUtils.intersection(tbBizPlaces, listWithoutDup);
        System.out.println(JSON.toJSONString(intersectionCollection));

        Collection disjunctionCollection = CollectionUtils.disjunction(tbBizPlaces, listWithoutDup);
        System.out.println(JSON.toJSONString(disjunctionCollection));


        /*Collection<TbBizPlace> collection = Collections2.filter(listWithoutDup, new Predicate<TbBizPlace>(){
            @Override
            public boolean apply(@Nullable TbBizPlace input) {
                return tbBizPlaces.contains(input);
            }
        });
        System.out.println(JSON.toJSONString(collection));*/


    }

    @Test
    public void test7(){
        searchService.updatePlaceIndex();
    }

}
